package main

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"io"
	"log"
	"net/http"
	"os"
)

func main() {
	// 禁止控制台颜色
	gin.DisableConsoleColor()

	// 还可以自定义日志格式
	gin.DebugPrintRouteFunc = func(httpMethod, absolutePath, handlerName string, nuHandlers int) {
		log.Printf("endpoint: %v %v %v %v\n", httpMethod, absolutePath, handlerName, nuHandlers)
	}

	// 将日志写入文件
	// 日志文件大小会限制吗
	// 一般会有专门的日志组件
	f, _ := os.Create("gin.log")
	gin.DefaultWriter = io.MultiWriter(f)

	r := gin.Default()
	r.GET("/ping", func(ctx *gin.Context) {
		zapLog()
		ctx.String(http.StatusOK, "pong")
	})

	r.Run(":8080")
}

func zapLog() {
	//logger := zap.NewExample()
	logger, _ := zap.NewDevelopment()
	logger.Debug("this is debug message")
	logger.Info("this is info message")
	logger.Info("this is info message with fileds",
		zap.Int("age", 24), zap.String("agender", "man"))
	logger.Warn("this is warn message")
	logger.Error("this is error message")
	//logger.Panic("this is panic message")
}
